/*
 * Copyright (C) 2009 Otávio Scherer Garcia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.technorati4j.entity;

import com.googlecode.technorati4j.enums.CosmosType;
import com.googlecode.technorati4j.enums.ResponseFormat;

/**
 * Request entity for cosmos API.
 * 
 * @author Otávio Scherer Garcia
 * @version $Revision$
 */
public final class CosmosRequest
    implements Request {

    /** The Technorati API key. */
    private String key;

    /** The base target URL for which you are searching. */
    private String url;

    /**
     * A value of link returns the freshest links referencing your target URL. A value of weblog returns the last set of
     * unique weblogs referencing your target URL.
     */
    private CosmosType type;

    /** Adjust the size of your result from the default value of 20 to between 1 and 100 results. */
    private int limit;

    /**
     * Adjust the range of your result set. Set this number to larger than zero and you will receive the portion of
     * Technorati's total result set ranging from start to start+limit. The default start value is 1.
     */
    private int start;

    /**
     * The default setting of yes Technorati returns links that are currently on a weblog's homepage. Set this parameter
     * to no if you would like to receive all links to the given URL regardless of their current placement on the source
     * blog.
     */
    private boolean current;

    /**
     * The default setting of xml returns results using XML and Technorati's proprietary doctype. Set this parameter to
     * rss to receive results in the RSS 2.0 markup format.
     */
    private ResponseFormat format;

    /**
     * The default setting of 0 returns no user information about each weblog included in the result set when available.
     * Set this parameter to 1 to include Technorati member data in the result set when a weblog in your result set has
     * been successfully claimed by a member of Technorati.
     */
    private boolean claim;

    /**
     * The default setting of 1 highlights the citation of the given URL within the weblog excerpt. Set this parameter
     * to 0 to apply no special markup to the blog excerpt.
     */
    private boolean highlight;

    /**
     * The default constructor.
     */
    public CosmosRequest() {
    }

    /**
     * Constructor with mandatory attributes.
     * 
     * @param key The key parameter.
     * @param url The url parameter.
     */
    public CosmosRequest(String key, String url) {
        setKey(key);
        setUrl(url);
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public CosmosType getType() {
        return type;
    }

    public void setType(CosmosType type) {
        this.type = type;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public boolean isCurrent() {
        return current;
    }

    public void setCurrent(boolean current) {
        this.current = current;
    }

    public ResponseFormat getFormat() {
        return format;
    }

    public void setFormat(ResponseFormat format) {
        this.format = format;
    }

    public boolean isClaim() {
        return claim;
    }

    public void setClaim(boolean claim) {
        this.claim = claim;
    }

    public boolean isHighlight() {
        return highlight;
    }

    public void setHighlight(boolean highlight) {
        this.highlight = highlight;
    }

    public String getParameters() {
        final StringBuilder str = new StringBuilder();

        str.append("key=");
        str.append(key);

        str.append("&url=");
        str.append(url);

        if (type != null) {
            str.append("&type=");
            str.append(type);
        }

        if (limit > 0 && limit <= 100) {
            str.append("&limit=");
            str.append(limit);
        }

        if (start > 0) {
            str.append("&start=");
            str.append(start);
        }

        if (current) {
            str.append("&current=yes");
        }

        if (format != null) {
            str.append("&format=");
            str.append(format);
        }

        if (claim) {
            str.append("&claim=1");
        }

        if (highlight) {
            str.append("&highlight=");
        }

        return str.toString();
    }
}
